package com.secondkill.messagequeue.rabbitmq;

import com.secondkill.config.RabbitMqConfig;
import com.secondkill.redis.RedisService;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * Created by zhangkai12 on 2018/7/6.
 */
@Service
public class RabbitMqSender {

    private final AmqpAdmin amqpAdmin;
    private final AmqpTemplate amqpTemplate;

    @Autowired
    public RabbitMqSender(AmqpAdmin amqpAdmin, AmqpTemplate amqpTemplate) {
        this.amqpAdmin = amqpAdmin;
        this.amqpTemplate = amqpTemplate;
    }

    public void sendMsg(String topic, String msg) {
        amqpTemplate.convertAndSend(topic, msg);
    }


    /**
     *  TopicExchange Sender
     */
    public void sendTopicExchangeMsg(String routingKey, String msg) {
        amqpTemplate.convertAndSend(RabbitMqConfig.TOPICEXCHANGE, routingKey, msg);
    }

    public void sendFanoutExchange(String msg) {
        amqpTemplate.convertAndSend(RabbitMqConfig.FANOUTEXCHANGE,"",msg);
    }

    public void sendMiaoshaMessage(MiaoshaMessage mm) {
        amqpTemplate.convertAndSend(RabbitMqConfig.MIAOSHA_QUEUE, RedisService.TtoString(mm));
    }
}